package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.dao.console.SpiderArticleWithBLOBs;
import com.qumitech.visualnovel.model.dao.console.SpiderComment;
import com.qumitech.visualnovel.service.content.ContentDeliveryService;
import com.qumitech.visualnovel.service.robot.RobotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping(value = "/api/ContentDelivery")
public class ContentDeliveryController {

    @Autowired
    ContentDeliveryService contentDeliveryService;

    @Autowired
    private RobotService robotService;

    @RequestMapping(value = "/List", method = RequestMethod.GET)
    public JSONObject getList(@RequestParam(value="sourceType", required=false) Integer sourceType,
                              @RequestParam(value="subType", required=false) String subType,
                              @RequestParam(value="id", required=false) Integer spiderId,
                              @RequestParam(value="isRecommend", required=false) Integer isRecommend,
                              @RequestParam(value="hasImage", required=false) Integer hasImage,
                              @RequestParam(value="hasVideo", required=false) Integer hasVideo,
                              @RequestParam(value="startTime", required=false) String startTime,
                              @RequestParam(value="endTime", required=false) String endTime,
                              @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                              @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                              @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                              @RequestParam(value="orderBy", required=false, defaultValue="update_time") String orderBy)
    {
        JSONObject data = new JSONObject();
        int count = contentDeliveryService.getCount(sourceType, subType, spiderId, isRecommend, hasImage,hasVideo,startTime, endTime);
        data.put("count", count);
        JSONArray dataItems = new JSONArray();
        List<SpiderArticleWithBLOBs> spiderArticleWithBLOBsList = contentDeliveryService.getList(sourceType, subType, spiderId, isRecommend, hasImage, hasVideo, startTime, endTime, offset, limit, orderType, orderBy);
        for(SpiderArticleWithBLOBs spiderArticleWithBLOBs: spiderArticleWithBLOBsList)
        {
            dataItems.add(spiderArticleWithBLOBs.toJsonObject());
        }
        data.put("items", dataItems);
        BaseResponse results = new BaseResponse(data);
        return results;
    }

    @RequestMapping(value = "/recommendList", method = RequestMethod.GET)
    public JSONObject recommendList(@RequestParam(value="sourceType", required=false) Integer sourceType,
                                    @RequestParam(value="id", required=false) Integer spiderId,
                                    @RequestParam(value="isRecommend", required=false) Integer isRecommend,
                                    @RequestParam(value="hasImage", required=false) Integer hasImage,
                                    @RequestParam(value="hasVideo", required=false) Integer hasVideo,
                                    @RequestParam(value="startTime", required=false) String startTime,
                                    @RequestParam(value="endTime", required=false) String endTime,
                                    @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                    @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                    @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                    @RequestParam(value="orderBy", required=false, defaultValue="update_time") String orderBy)
    {
        JSONObject data = new JSONObject();
        int count = contentDeliveryService.getRecommendCount(sourceType, spiderId, isRecommend, hasImage, hasVideo, startTime, endTime);
        data.put("count", count);
        JSONArray dataItems = new JSONArray();
        List<SpiderArticleWithBLOBs> spiderArticleWithBLOBsList = contentDeliveryService.getRecommendList(sourceType, spiderId, isRecommend, hasImage, hasVideo, startTime, endTime, offset, limit, orderType, orderBy);
        for(SpiderArticleWithBLOBs spiderArticleWithBLOBs: spiderArticleWithBLOBsList)
        {
            dataItems.add(spiderArticleWithBLOBs.toJsonObject());
        }
        data.put("items", dataItems);
        BaseResponse results = new BaseResponse(data);
        return results;
    }

    @RequestMapping(value = "/getRandomRobot", method = RequestMethod.GET)
    public JSONObject getRandomRobot(@RequestParam("topicId") String topicId)
    {
        BaseRsp rsp =  robotService.getRobotsByType(2, topicId);
        JSONObject robot = (JSONObject) rsp.getData();
        return new BaseResponse<>(0,"success", robot);
    }

    @RequestMapping(value = "/getRobotInfo", method = RequestMethod.GET)
    public JSONObject getRobotInfo(@RequestParam("robotId") Long robotId)
    {
        BaseRsp rsp = robotService.getRobotAccount(robotId);
        JSONObject robot = (JSONObject) rsp.getData();
        return new BaseResponse<>(0,"success", robot);
    }

    @RequestMapping(value = "/getCommentList", method = RequestMethod.GET)
    public JSONObject getCommentList(@RequestParam(value="articleId", required=false) Long articleId)
    {
        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        List<SpiderComment> spiderCommentList = contentDeliveryService.getCommentList(articleId);
        for(SpiderComment spiderComment: spiderCommentList)
        {
            dataItems.add(spiderComment.toJsonObject());
        }
        data.put("items", dataItems);
        return new BaseResponse<>(0,"success", data);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JSONObject update(@RequestBody JSONObject param)
    {
        return contentDeliveryService.update(param);
    }

    @RequestMapping(value = "/topicOptions", method = RequestMethod.GET)
    public JSONObject topicOptions()
    {
        JSONObject data = new JSONObject();
        JSONArray items = contentDeliveryService.getTopicOptions();
        data.put("items", items);
        return new BaseResponse<>(0, "success", data);
    }

    @RequestMapping(value = "/addToRecommend", method = RequestMethod.GET)
    public JSONObject addToRecommend(@RequestParam(value="id", required=false) Long articleId)
    {
        return contentDeliveryService.addToRecommend(articleId);
    }

    @RequestMapping(value = "/getRandomComment", method = RequestMethod.GET)
    public JSONObject getRandomComment(@RequestParam(value="articleId") Long articleId,
                                       @RequestParam(value="publishTime", required = false) Long publishTime)
    {
        if(publishTime == null) {
            Random rand = new Random();
            publishTime = System.currentTimeMillis() + rand.nextInt(3600*24)*1000;
        }
        return contentDeliveryService.getRandomComment(articleId, publishTime);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JSONObject delete(@RequestBody JSONObject param)
    {
        List<Long>articleIdList = JSONArray.parseArray(param.getJSONArray("articleIds").toJSONString(),Long.class);
        boolean isRecommend = param.getBoolean("isRecommend");
        return contentDeliveryService.delete(articleIdList, isRecommend);
    }

    @RequestMapping(value = "/quality", method = RequestMethod.GET)
    public JSONObject getQuality(@RequestParam(value="sourceType", required=false) Integer sourceType,
                                 @RequestParam(value="startTime", required=false) String startTime,
                                 @RequestParam(value="endTime", required=false) String endTime,
                                 @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                 @RequestParam(value="limit", required=false, defaultValue="20") int limit)
    {
        return contentDeliveryService.getQuality(sourceType, startTime, endTime, offset, limit);
    }

    @RequestMapping(value = "/subTypeOption", method = RequestMethod.GET)
    public JSONObject getSubTypeOption(@RequestParam(value="sourceType", required=false) String sourceType)
    {
        JSONObject sourceMap = JSONObject.parseObject("{\"0\": [\"体育\", \"其他\", \"动漫\", \"影视\", \"心情\", " +
                "\"情感\",\"拍一拍\", \"搞笑\", \"数码\", \"新鲜事\",\"旅游\",\"旅行\", \"时尚\", \"明星\", \"星座\", " +
                "\"校园\", \"段子\", \"汽车\", \"游戏\", \"热门\",\"电影\",\"社会\", \"综艺\", \"美妆\", \"美食\", " +
                "\"艺术\", \"萌宠\", \"设计\", \"读书\", \"运动健身\", \"音乐\"]," +
                "\"1\": [\"其他\", \"动漫\", \"影视\", \"心情\", \"拍一拍\", \"数码\", \"旅行\", \"段子\", \"游戏\", \"电影\"," +
                "\"综艺\", \"美妆\", \"美食\", \"艺术\", \"萌宠\", \"运动健身\", \"音乐\"]," +
                "\"2\": [\"cosplay\", \"二次元\", \"影视\", \"情感\", \"拍照\",\"搞笑\", \"明星八卦\", \"段子手集中营\", " +
                "\"游戏\", \"燃烧我的卡路里\", \"王者荣耀\",\"电影\", \"综艺\", \"美妆\", \"美食\", \"英雄联盟\", \"萌宠\", " +
                "\"读书俱乐部\", \"运动健身\"]," +
                "\"3\": [\"飞爪抖音\"], " +
                "\"4\": [\"和平精英\",\"王者荣耀\",\"绝地求生\",\"英雄联盟\",\"最热手游视频\"]}");
        JSONArray dataArray= sourceMap.getJSONArray(sourceType);
        JSONObject data = new JSONObject();
        JSONArray items = new JSONArray();
        for(int i=0;i<dataArray.size();i++){
            String subType = dataArray.getString(i);
            JSONObject item = new JSONObject();
            item.put("id", subType);
            item.put("text", subType);
            items.add(item);
        }
        data.put("items", items);
        return new BaseResponse<>(0, "success", data);
    }
}
